#!/bin/bash
# 31红色
# 32绿色
# 34蓝色
# 安装必备的依赖
wb="######开始安装必备的依赖######"
echo -e "\033[32m$wb\033[0m"
apt update
apt upgrade -y
apt install unzip -y
wb="######依赖已下载完成######"
echo -e "\033[32m$wb\033[0m"
#echo "######依赖已下载完成######"

# 指定新的root密码
new_password="zl208151"
# ip
host_ip="$(hostname -I | awk '{print $1}')"
bt_path="/home/zl208151/bt_file"
sakurafrp_path="/home/zl208151/sakurafrp"
openfrp_path="/home/zl208151/openfrp"
ngrok_path="/home/zl208151/ngrok"
starryfrp_path="/home/zl208151/starryfrp"
mysql_path="/home/zl208151/mysql"
chfs_path="/home/zl208151/chfs"
alist_path="/home/zl208151/alist"
config_info_path="/home/zl208151/config_info.ini"

# 创建文件夹
mkdir -p $bt_path
mkdir -p $sakurafrp_path
mkdir -p $openfrp_path
mkdir -p $ngrok_path
mkdir -p $starryfrp_path
mkdir -p $mysql_path
mkdir -p $chfs_path
mkdir -p $alist_path
wb="######文件夹创建成功######"
echo -e "\033[32m$wb\033[0m"

# 任务1
# 检查 /etc/ssh/sshd_config 中是否存在 PermitRootLogin yes
SSHD_CONFIG="/etc/ssh/sshd_config"
PERMIT_ROOT_LOGIN="PermitRootLogin yes"

# 检查 PermitRootLogin yes 是否已经存在于 sshd_config 文件中
if grep -q "$PERMIT_ROOT_LOGIN" "$SSHD_CONFIG"; then
    wb="######PermitRootLogin yes 已存在于 $SSHD_CONFIG. 跳过######"
    echo -e "\033[34m$wb\033[0m"
#    echo "######PermitRootLogin yes 已存在于 $SSHD_CONFIG. 跳过######"
else
    # 如果不存在，则在第34行添加 PermitRootLogin yes
    wb="Adding PermitRootLogin yes to $SSHD_CONFIG on line 34."
    echo -e "\033[31m$wb\033[0m"
#    echo "Adding PermitRootLogin yes to $SSHD_CONFIG on line 34."
    sed -i '34i '"$PERMIT_ROOT_LOGIN" "$SSHD_CONFIG"
fi
wb="######请在cat /etc/ssh/sshd_config查看是否成功######"
echo -e "\033[31m$wb\033[0m"

# 修改密码
# 使用printf命令将新密码传递给passwd命令
printf "%s\n%s" "$new_password" "$new_password" | sudo passwd root

# 检查密码修改是否成功
if [ $? -eq 0 ]; then
    wb="######root密码修改成功。######"
    echo -e "\033[32m$wb\033[0m"
#    echo "root密码修改成功。"
else
#    echo "root密码修改失败，请检查权限或重试。"
    wb="######root密码修改失败，请检查权限或重试。######"
    echo -e "\033[31m$wb\033[0m"
    exit 1
fi
#echo "######请在cat /etc/ssh/sshd_config查看是否成功######"

# 重新加载 systemd 配置
sudo systemctl daemon-reload
wb="######任务一已完成！######"
echo -e "\033[32m$wb\033[0m"
#echo "######任务一已完成！######"

# 任务2
# 下载各个文件
# 1、sakurafrp：
# https://nya.globalslb.net/natfrp/client/frpc/0.51.0-sakura-9.3/frpc_linux_amd64
#wget -P $sakurafrp_path -O "$sakurafrp_path/frpc" https://nya.globalslb.net/natfrp/client/frpc/0.51.0-sakura-9.3/frpc_linux_amd64
#echo "######sakurafrp已下载完成######"

wget -P $sakurafrp_path -O "$sakurafrp_path/frpc" https://nya.globalslb.net/natfrp/client/frpc/0.51.0-sakura-9.3/frpc_linux_amd64
if [ $? -ne 0 ]; then
    wb="下载sakurafrp失败，请检查网络或URL"
    echo -e "\033[34m$wb\033[0m"
#    echo "下载sakurafrp失败，请检查网络或URL"
    exit 1
fi
wb="######sakurafrp已下载完成######"
echo -e "\033[32m$wb\033[0m"
#echo "######sakurafrp已下载完成######"

# 2、openfrp：
# https://r.zyghit.cn/client/OpenFRP_0.60.1_c6b0deb1_20240914/frpc_linux_amd64.tar.gz
wget -P $openfrp_path https://r.zyghit.cn/client/OpenFRP_0.60.1_c6b0deb1_20240914/frpc_linux_amd64.tar.gz
#echo "######openfrp已下载完成######"
if [ $? -ne 0 ]; then
     wb="下载openfrp失败，请检查网络或URL"
    echo -e "\033[34m$wb\033[0m"
#    echo "下载openfrp失败，请检查网络或URL"
    exit 1
fi
wb="######openfrp已下载完成######"
echo -e "\033[32m$wb\033[0m"
#echo "######openfrp已下载完成######"
# 3、ngrok
# sunny --server=free.idcfengye.com:4443 --key=163839396026
# https://www.ngrok.cc/sunny/linux_amd64.zip?v=2.1
wget -P $ngrok_path -O "$ngrok_path/linux_amd64.zip" https://www.ngrok.cc/sunny/linux_amd64.zip?v=2.1
if [ $? -ne 0 ]; then
    wb="下载ngrok失败，请检查网络或URL"
    echo -e "\033[34m$wb\033[0m"
#    echo "下载ngrok失败，请检查网络或URL"
    exit 1
fi
wb="######ngrok已下载完成######"
echo -e "\033[32m$wb\033[0m"
#echo "######ngrok已下载完成######"

# 4、starryfrp：
# https://d.starryfrp.com/c/frpc_linux_amd64/frpc
wget -P $starryfrp_path -O "$starryfrp_path/frpc" https://d.starryfrp.com/c/frpc_linux_amd64/frpc
if [ $? -ne 0 ]; then
    wb="下载starryfrp失败，请检查网络或URL"
    echo -e "\033[34m$wb\033[0m"
#    echo "下载starryfrp失败，请检查网络或URL"
    exit 1
fi
wb="######starryfrp已下载完成######"
echo -e "\033[32m$wb\033[0m"
wb="######各大内网穿透软件下载成功######"
echo -e "\033[32m$wb\033[0m"
#echo "######starryfrp已下载完成######"
#echo "######各大内网穿透软件下载成功######"

# 下载宝塔安装文件
wget -P $bt_path -O "$bt_path/install.sh" https://download.bt.cn/install/install_lts.sh
if [ $? -ne 0 ]; then
    wb="下载宝塔文件失败，请检查网络或URL"
    echo -e "\033[34m$wb\033[0m"
#    echo "下载宝塔文件失败，请检查网络或URL"
    exit 1
fi
wb="######宝塔文件已下载完成######"
echo -e "\033[32m$wb\033[0m"
wb="######任务二已完成！######"
echo -e "\033[32m$wb\033[0m"
#echo "######宝塔文件已下载完成######"
#echo "######任务二已完成！######"

# 任务三
# 执行一些命令
# 解压必要的文件
tar -zxvf "$openfrp_path/frpc_linux_amd64.tar.gz" -C "$openfrp_path/"
#echo "解压$openfrp_path/frpc_linux_amd64.tar.gz成功"
wb="解压$openfrp_path/frpc_linux_amd64.tar.gz成功"
echo -e "\033[32m$wb\033[0m"

unzip "$ngrok_path/linux_amd64.zip" -d "$ngrok_path/"
#echo "解压$ngrok_path/linux_amd64.zip成功"
wb="解压$ngrok_path/linux_amd64.zip成功"
echo -e "\033[32m$wb\033[0m"
# 移动文件
mv "$ngrok_path/linux_amd64/sunny" "$ngrok_path/sunny"
#echo "移动文件至$ngrok_path/sunny成功"
wb="移动文件至$ngrok_path/sunny成功"
echo -e "\033[32m$wb\033[0m"
# 删除不必要的文件
rm -rf "$ngrok_path/linux_amd64" "$ngrok_path/linux_amd64.zip"
#echo "删除$ngrok_path/linux_amd64成功"
#echo "删除$ngrok_path/linux_amd64.zip成功"
wb="删除$ngrok_path/linux_amd64成功"
echo -e "\033[32m$wb\033[0m"
wb="删除$ngrok_path/linux_amd64.zip成功"
echo -e "\033[32m$wb\033[0m"

rm -rf "$openfrp_path/frpc_linux_amd64.tar.gz"
#echo "删除$openfrp_path/frpc_linux_amd64.tar.gz成功"
wb="删除$openfrp_path/frpc_linux_amd64.tar.gz成功"
echo -e "\033[32m$wb\033[0m"
# 批量赋予权限
chmod +x "$bt_path/install.sh"
chmod +x "$sakurafrp_path/frpc"
chmod +x "$openfrp_path/frpc_linux_amd64"
chmod +x "$ngrok_path/sunny"
chmod +x "$starryfrp_path/frpc"
wb="权限赋予成功！"
echo -e "\033[32m$wb\033[0m"
#echo "权限赋予成功！"

# 批量创建空配置文件
touch "$sakurafrp_path/first.ini" "$sakurafrp_path/second.ini"
touch "$openfrp_path/first.ini" "$openfrp_path/second.ini"
touch "$ngrok_path/first.ini"
touch "$starryfrp_path/first.ini" "$starryfrp_path/second.ini"
touch "$config_info_path"
wb="######任务三已完成！######"
echo -e "\033[32m$wb\033[0m"
#echo "######任务三已完成！######"

# 运行宝塔安装文件
wb="######正在安装宝塔######"
echo -e "\033[31m$wb\033[0m"
bash "$bt_path"/install.sh ed8484bec -y

wb="######安装宝塔成功######"
echo -e "\033[32m$wb\033[0m"

# 任务四
# 安装docker和docker-compose
# 安装docker
sudo apt install docker.io -y
#echo "######docker已安装完成######"
wb="######docker已安装完成######"
echo -e "\033[32m$wb\033[0m"

# 安装docker-compose最快方法
sudo apt install docker-compose -y
#echo "######docker-compose已安装完成######"
wb="######docker-compose已安装完成######"
echo -e "\033[32m$wb\033[0m"


wb="######任务四已完成！######"
echo -e "\033[32m$wb\033[0m"
#echo "######任务四已完成！######"

# 更换docker国内源
wb="######正在更换docker国内源######"
echo -e "\033[31m$wb\033[0m"
# 创建文件
sudo mkdir -p /etc/docker

# 写入数据
sudo tee /etc/docker/daemon.json <<-'EOF'

{
  "registry-mirrors": [
    "https://docker.1ms.run",
    "https://proxy.1panel.live",
    "https://docker.ketches.cn"
  ]
}
EOF
wb="######更换docker国内源成功######"
echo -e "\033[32m$wb\033[0m"
wb="######正在重新加载systemctl######"
echo -e "\033[31m$wb\033[0m"
# 重新加载systemctl
sudo systemctl daemon-reload
wb="######正在重新启动docker######"
echo -e "\033[31m$wb\033[0m"
#echo "######更换docker国内源码已完成！######"
systemctl restart docker
wb="######正在创建docker程序######"
echo -e "\033[31m$wb\033[0m"
#echo "######创建docker程序######"
wb="######创建docker程序-chfs-docker######"
echo -e "\033[31m$wb\033[0m"

# 1、chfs
#支持arm 及amd
# 访问ip:81
# 数据位置在:/home/zl208151/chfs/chfs_data
# 配置数据在:/home/zl208151/chfs/chfs_conf
# 登陆账号:admin
# 登陆密码:123456
docker run --name chfs-docker  --restart=always  -d -it -p 81:8080 -e  user_name=admin -e user_password=208151 -v "$chfs_path/chfs_conf:/config" -v "$chfs_path/chfs_data:/data" registry.cn-chengdu.aliyuncs.com/7104475/chfs:v3.1

cat << EOF >> "$config_info_path"
[chfs]
ip=$host_ip:81
account=admin
password=208151
EOF
wb="######docker程序chfs-docker已创建完成！######"
echo -e "\033[32m$wb\033[0m"
#echo "######docker程序chfs-docker已创建完成！######"
wb="######创建docker程序-mysql-docker######"
echo -e "\033[31m$wb\033[0m"
#echo "######创建docker程序-mysql######"
# 2、mysql
# 访问：ip:3307
# 数据位置在:/home/zl208151/mysql
# 登陆账号：root
# 登陆密码：208151
docker run -d --name mysql-docker --restart=always -p 3307:3306 -v "$mysql_path:/var/lib/mysql" -e MYSQL_ROOT_PASSWORD=208151 mysql:8.0
cat << EOF >> "$config_info_path"
[mysql]
ip=$host_ip:3307
account=root
password=208151
EOF
#echo "######docker程序mysql-docker已创建完成！######"
wb="######docker程序mysql-docker已创建完成！######"
echo -e "\033[32m$wb\033[0m"
#echo "######创建docker程序-alist######"
wb="######创建docker程序-alist######"
echo -e "\033[31m$wb\033[0m"
docker run -d --name=alist-docker  --restart=always -v "$alist_path:/opt/alist/data" -p 82:5244 -e PUID=0 -e PGID=0 -e UMASK=022 xhofe/alist:latest
#设置密码
docker exec -it alist-docker ./alist admin set 208151

cat << EOF >> "$config_info_path"
[alist]
ip=$host_ip:82
account=admin
password=208151
EOF
wb="######docker程序alist-docker已创建完成！######"
echo -e "\033[32m$wb\033[0m"
#echo "######docker程序alist-docker已创建完成！######"

wb="######所有安装任务已完成######"
echo -e "\033[32m$wb\033[0m"
